package com.junjun.ssm.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.junjun.ssm.dao.GradeDao;
import com.junjun.ssm.dao.UserDao;
import com.junjun.ssm.dto.OrderDto;
import com.junjun.ssm.entity.GradeEntity;
import com.junjun.ssm.entity.MovieEntity;
import com.junjun.ssm.entity.OrderEntity;
import com.junjun.ssm.entity.UserEntity;
import com.junjun.ssm.service.IMeiTuanService;
import com.junjun.ssm.service.IOrderService;
import com.junjun.ssm.service.ToMovieService;
import com.junjun.ssm.util.ConvertBeanUtil;
import com.junjun.ssm.util.CreateToken;
import com.junjun.ssm.util.Verifier;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@RequestMapping("meiTuan")
@RestController
public class MeiTuanController {

    @Autowired
    ToMovieService toMovieService;

    @Autowired
    IMeiTuanService meiTuanService;


    @Autowired
    UserDao userDao;


    @Autowired
    RabbitTemplate rabbitTemplate;


    @Autowired
    IOrderService orderService;

    @Autowired
    GradeDao gradeDao;

    @Autowired
    RedisTemplate<String, Object> redisTemplate;

//    @RequestMapping("/userSession")
//    public UserEntity sessionUser(HttpServletRequest request) {
//        UserEntity u = (UserEntity) request.getSession().getAttribute("user");
//        return u;
//
//
//    }

@RequestMapping("queryText")
public String queryText(){
    return toMovieService.queryText();
}
    @RequestMapping("queryText1")
    public String queryText1(){
        return meiTuanService.queryText1();
    }

    @RequestMapping("/login")
    public String loginUser(String userName, String password) {
        UserEntity userBean = new UserEntity();
        userBean.setUserName(userName);
        userBean.setPassword(password);
        UserEntity u = userDao.findByNameAndPassword(userName, password);
        String token = null;
        if (u != null) {
            token = CreateToken.createToken(JSON.toJSONString(u));
            return token;
        }
        return "error";


    }


    @PostMapping("queryToAdd")
    public List<MovieEntity> queryToAdd() {

        return meiTuanService.query();
    }


//    @Scheduled(cron = "0 15 8 ? * * 2023") //定时调度
//    //@PostMapping("addMeiTuan")
//    public void addMeiTuan() {
//
//        meiTuanService.addMeiTuan();
//    }


    @PostMapping("addOrder")
    public void addOrder(@RequestBody OrderEntity orderEntity, HttpServletRequest request) {
        //用户下单保存本地数据库
        String token = request.getHeader("token");
        String result = Verifier.verifierJwt(token);
        UserEntity user = JSONObject.parseObject(result, UserEntity.class);
        GradeEntity gradeEntity = gradeDao.findByGradeName(user.getUserGrade());


        LocalDateTime localDateTime = LocalDateTime.now();
        orderEntity.setPurchaseTime(String.valueOf(localDateTime));
        orderEntity.setUserId(user.getUserID());
        orderEntity.setUserName(user.getUserName());
        orderEntity.setPrice(orderEntity.getPrice() * gradeEntity.getDiscount());
        orderEntity.setPurchaseNum(1);
        orderEntity.setState(0);
        //保存本地数据库
        orderService.save(orderEntity);


        //放入redis数据库
        String key = "order";
        String json = JSON.toJSONString(orderEntity);
        redisTemplate.opsForList().leftPush(key, json);

  /*
    //开启一个线程，从redis取出数据放入mq
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                while (true) {
                    String order = (String) redisTemplate.opsForList().rightPop(key);
                    if (order != null && order != "") {
                        OrderEntity orderEntity1 = JSON.parseObject(order, OrderEntity.class);
                        OrderDto orderDto = ConvertBeanUtil.copy(orderEntity1, OrderDto.class);
                        String json1 = JSON.toJSONString(orderEntity1);
                        meiTuanService.send(orderDto, json1);

                    }
                }

            }).start();
        }
*/

//        //保存本地数据库后  发送给MQ
//        OrderDto orderDto = ConvertBeanUtil.copy(orderEntity, OrderDto.class);
//        String json = JSON.toJSONString(orderEntity);
//        meiTuanService.send(orderDto, json);


    }
}